package cn.stylefeng.guns.modular.business.frequency.orderroyalty.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.exception.enums.StatusExceptionEnum;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.modular.business.frequency.orderroyalty.entity.OrderRoyalty;
import cn.stylefeng.guns.modular.business.frequency.orderroyalty.mapper.OrderRoyaltyMapper;
import cn.stylefeng.guns.modular.business.frequency.orderroyalty.model.param.OrderRoyaltyParam;
import cn.stylefeng.guns.modular.business.frequency.orderroyalty.service.OrderRoyaltyService;
import cn.stylefeng.guns.modular.business.frequency.orderroyalty.enums.OrderRoyaltyExceptionEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 提成记录表 服务实现类
 *
 * @author yujie
 * @date 2023/05/09 17:16
 */
@Service
public class OrderRoyaltyServiceImpl extends ServiceImpl<OrderRoyaltyMapper, OrderRoyalty> implements OrderRoyaltyService {

    @Override
    public PageResult<OrderRoyalty> page(OrderRoyaltyParam orderRoyaltyParam) {

        // 构造条件
        LambdaQueryWrapper<OrderRoyalty> queryWrapper = new LambdaQueryWrapper<>();


        // 查询分页结果
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<OrderRoyalty> list(OrderRoyaltyParam orderRoyaltyParam) {

        // 构造条件
        LambdaQueryWrapper<OrderRoyalty> queryWrapper = new LambdaQueryWrapper<>();


        return this.list(queryWrapper);
    }

    @Override
    public void add(OrderRoyaltyParam orderRoyaltyParam) {

        // 将dto转为实体
        OrderRoyalty orderRoyalty = new OrderRoyalty();
        BeanUtil.copyProperties(orderRoyaltyParam, orderRoyalty);


        this.save(orderRoyalty);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(OrderRoyaltyParam orderRoyaltyParam) {

        // 根据id查询实体
        OrderRoyalty orderRoyalty = this.queryOrderRoyalty(orderRoyaltyParam);

        // 更新实体
        this.updateById(orderRoyalty);

    }

    @Override
    public void edit(OrderRoyaltyParam orderRoyaltyParam) {

        // 根据id查询实体
        OrderRoyalty orderRoyalty = this.queryOrderRoyalty(orderRoyaltyParam);

        // 请求参数转化为实体
        BeanUtil.copyProperties(orderRoyaltyParam, orderRoyalty);

        // 不能修改状态，用修改状态接口修改状态
        orderRoyalty.setStatus(null);

        this.updateById(orderRoyalty);
    }

    @Override
    public OrderRoyalty detail(OrderRoyaltyParam orderRoyaltyParam) {
        return this.queryOrderRoyalty(orderRoyaltyParam);
    }

    @Override
    public void changeStatus(OrderRoyaltyParam orderRoyaltyParam) {
        Long id = orderRoyaltyParam.getId();
        Integer status = orderRoyaltyParam.getStatus();

        // 更新枚举，更新只能更新未删除状态的
        LambdaUpdateWrapper<OrderRoyalty> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper
            .eq(OrderRoyalty::getId, id).set(OrderRoyalty::getStatus, status);
        boolean update = this.update(updateWrapper);
        if (!update) {
            throw new ServiceException(StatusExceptionEnum.UPDATE_STATUS_ERROR);
        }
    }


    /**
     * 获取提成记录表
     *
     * @author yujie
     * @since 2023/05/09 17:16
     */
    private OrderRoyalty queryOrderRoyalty(OrderRoyaltyParam orderRoyaltyParam) {
        OrderRoyalty orderRoyalty = this.getById(orderRoyaltyParam.getId());
        if (ObjectUtil.isEmpty(orderRoyalty)) {
            throw new ServiceException(OrderRoyaltyExceptionEnum.NOT_EXISTED);
        }
        return orderRoyalty;
    }
}